package com.xxxx.controller;

import com.xxxx.dto.AdminDto;
import com.xxxx.pojo.Admin;
import com.xxxx.pojo.AdminLoginParam;
import com.xxxx.pojo.R;
import com.xxxx.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author achao
 * @version 1.0
 */

@RestController
@Api(tags = "LoginController")
public class LoginController {

    @Autowired
    private AdminService adminService;

    /**
     *
     * @param adminLoginParam
     * @param request
     * @return
     */
    @ApiOperation(value = "登陆后返回token")
    @PostMapping("/login")
    public R login(@RequestBody AdminDto adminLoginParam, HttpServletRequest request) {
        return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), adminLoginParam.getCode(),request);
    }

    @ApiOperation(value = "获取当前登陆用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal) {
        if(null==principal)
            return null;
        String username=principal.getName();
       Admin admin= adminService.getAdminByUsername(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return admin;
    }

    @ApiOperation(value = "退出登陆")
    @PostMapping("/logout")
    public R logout() {
        return R.success("注销成功！");
    }
}
